package com.tao.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class TimeSimpleUtil {

    static Map<String, SimpleDateFormat> formatMap = new HashMap<>();
    private static String defaultFormat = "yyyy-MM-dd HH:mm:ss";

    @SuppressLint("SimpleDateFormat")
    public static String timeStamp2Date(long timeStamp, String formats) throws Exception {
        // Long timestamp = Long.parseLong(timestampString);
        String date = getFormats(formats).format(new Date(timeStamp));
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public static String timeStamp2Date(long timeStamp) throws Exception {
        // Long timestamp = Long.parseLong(timestampString);
        String date = getFormats(defaultFormat).format(new Date(timeStamp));
        return date;
    }

    private static SimpleDateFormat getFormats(String formats) {
        if (formatMap.containsKey(formats)) {
            return formatMap.get(formats);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(formats);
        formatMap.put(formats,dateFormat);
        return dateFormat;
    }

    @SuppressLint("SimpleDateFormat")
    public static String TimeNowWithFormat(String format) throws Exception {
        return timeStamp2Date(System.currentTimeMillis(), format);
    }  public static String TimeNowWithFormat( ) throws Exception {
        return timeStamp2Date(System.currentTimeMillis(), defaultFormat);
    }

    public static Date parse(String s, String msg) throws ParseException {
        return getFormats(s).parse(msg);
    }

    public static Date parse(String msg) throws ParseException {
        return getFormats(defaultFormat).parse(msg);
    }
}
